// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import CombatComponent from "../../Combats/CombatComponent";
import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import GateManager, { GateEvent } from "../../Models/Gates/GateManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GateUI extends cc.Component {
    @property(cc.Label)
    gateTxt: cc.Label = null;
    @property(cc.Label)
    waveTxt: cc.Label = null;
    @property(cc.Label)
    enemyNumTxt: cc.Label = null;
    @property(cc.Label)
    timeTxt: cc.Label = null;
    @property(cc.Node)
    tipsNode: cc.Node = null;
    @property(cc.Label)
    tipsTxt: cc.Label = null;

    onLoad() {
        this.timeTxt.string = "0";
        this.tipsNode.active = false;
    }

    protected onEnable(): void {
        this.scheduleOnce(() => {
            this.drawGate();
        });

        GameEvent.eventNode.on(GameEvent.MAKEENEMY_EVENT, this.onMakeEnemy, this);
        GameEvent.eventNode.on(GateEvent.GATEUIUPDATA_EVENT, this.onGateUIUpdata, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(GameEvent.MAKEENEMY_EVENT, this.onMakeEnemy, this);
        GameEvent.eventNode.off(GateEvent.GATEUIUPDATA_EVENT, this.onGateUIUpdata, this);
    }
    onGateUIUpdata() {
        this.drawGate();
    }
    onMakeEnemy() {
        this.drawGate();
    }
    drawGate() {
        var userdata = SaveManager.single.userdata;
        this.gateTxt.string = "第@关".replace("@", userdata.gateEntity.currentGateId.toString());
        this.waveTxt.string = "" + (GateManager.single.currentWave + 1) + "/5";
        this.enemyNumTxt.string = "" + GateManager.single.enemyNum;
    }

    protected update(dt: number): void {
        if (!CombatComponent.startCombat) {
            return;
        }

        this.timeTxt.string = "" + GateManager.single.challengeTime.toFixed(1);
    }

    tipsClick(e: cc.Event.EventTouch, txt: string) {
        this.unschedule(this.hideTips);
        this.tipsNode.x = e.currentTarget.x;
        this.tipsNode.active = true;
        this.tipsTxt.string = txt;

        this.scheduleOnce(this.hideTips, 1);
    }
    hideTips() {
        this.tipsNode.active = false;
    }


}
